package com.qidian.mes.cal.service.Impl;

import com.qidian.mes.cal.convert.DateConvert;
import com.qidian.mes.cal.mapper.CalHolidayMapper;
import com.qidian.mes.cal.service.CalHolidayService;
import com.qidian.mes.pojo.CalHoliday;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;


@Service
public class CalHolidayServiceImpl implements CalHolidayService {
    @Autowired
    CalHolidayMapper calHolidayMapper;
    @Autowired
    DateConvert dateConvert;

    @Override
    //添加节假日
    public MESResult save(CalHoliday calHoliday) {
        //查看节假日是否存在
        Date date=new Date();
        date.setTime(calHoliday.getTheDay().getTime());
        CalHoliday ch=calHolidayMapper.selectByTheDay(new SimpleDateFormat("yyyy-MM-dd").format(date));
        //如果存在
        if (!Objects.isNull(ch)){
            calHoliday.setHolidayId(ch.getHolidayId());
          return MESResult.ok("修改成功",calHolidayMapper.update(calHoliday));
        }
        return MESResult.ok("设置成功",calHolidayMapper.save(calHoliday));
    }

    @Override
    //查询节假日
    public MESResult list() {
        return MESResult.ok(calHolidayMapper.list());
    }
}
